﻿#include "changepwd.h"
#include "ui_changepwd.h"

ChangePwd::ChangePwd(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ChangePwd)
{
    ui->setupUi(this);

}

ChangePwd::~ChangePwd()
{
    delete ui;
}

//判断两次输入的密码是否一致
bool ChangePwd::isPwdSame()
{
    if(this->newPwd.compare(this->surePwd) == 0){ //如果密码一致
        return true;
    }else{
        return false;
    }
}

void ChangePwd::initScene()
{
    setWindowTitle(GAME_NAME); //设置窗口标题

//    setWindowIcon(QIcon(GAME_ICON_)); //设置窗口图标
    //this->initQLabel(); //初始化标签
}

//接收信号显示修改密码界面
void ChangePwd::recShowChangePwd()
{
    //清空文本框中的内容
    ui->userName->clear();
    ui->oldPwd->clear();
    ui->newPwd->clear();
    ui->surePwd->clear();

    this->show();
}

void ChangePwd::on_back_clicked()
{
    emit this->sendShowMe(); //发送信号显示我界面
    this->hide();
}

void ChangePwd::on_change_clicked()
{
    //获取文本框值
    QString username = ui->userName->text();
    QString oldpwd = ui->oldPwd->text();
    QString newpwd = ui->newPwd->text();
    QString surepwd = ui->surePwd->text();

    this->newPwd = newpwd;
    this->surePwd = surepwd;

    //判断输入用户名是否重复
    int flag=0;
    if(this->userName.compare(username) == 0){ //如果用户名相等
        if(this->oldPwd.compare(oldpwd) == 0){ //如果旧密码相等
            flag = 1;
        }
    }

    //根据标志跳转窗口
    if(flag == 0){
            QMessageBox::warning(NULL, GAME_NAME, "用户名或密码错误！", QMessageBox::Yes);
        }else{
            if(this->isPwdSame()){ //判断两次输入的密码是否一致
                emit this->sendNewPwdToMe(this->newPwd); //发送信号传递新密码给我界面
                QMessageBox::information(NULL,GAME_NAME,"修改成功！",QMessageBox::Ok);
                emit this->sendShowMe(); //发送信号显示我界面
                this->hide();
            }else{
                QMessageBox::warning(NULL, GAME_NAME, "两次输入的密码不一致，请重新输入！", QMessageBox::Ok);
            }

        }
}
